from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relation
from divirt import sqlalchemy_meta_data
from divirt import logging
from host import Host

log = logging.getLogger('divirt.components.vm')

Base = declarative_base(metadata=sqlalchemy_meta_data)
class Vm(Base):
    # Table
    __tablename__ = 'vms'
    id = Column(Integer, primary_key=True)
    host_id = Column(Integer, ForeignKey('hosts.id'))
    disk = Column(String)
    memory = Column(String)
    vnc_port = Column(Integer)
    monitor_sock = Column(String)
    pid_file = Column(String)

    # Relations
    host = relation(Host, backref=backref('vms', order_by=id))

    def __init__(self, host_id=None, disk=None, memory=None, vnc_port=None,
                 monitor_sock=None, pid_file=None):
        self.host_id = host_id
        self.disk = disk
        self.memory = memory
        self.vnc_port = vnc_port
        self.monitor_sock = monitor_sock
        self.pid_file = pid_file

    def __repr__(self):
        return '<Vm(id=%s, host_id=%s, disk=%s, memory=%s, vnc_port=%s, ' \
            'monitor_sock=%s, pid_file=%s)>' \
            % (self.id, self.host_id, self.disk, self.memory, self.vnc_port,
               self.monitor_sock, self.pid_file)
